package com.szt.school.controller;

import com.github.pagehelper.PageInfo;
import com.szt.school.pojo.User;
import com.szt.school.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
@Slf4j
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable Long id, Model model){
        log.info("跳转到用户修改页-------------");
        User user = userService.queryById(id);
//        User user = new User();
        System.out.println(user);
        model.addAttribute("user",user);//key=value
        return "user/update";
    }

    @RequestMapping("update")
    public String update(User user,Model model){
        log.info("业务修改运行");
        userService.userUpdate(user);
        model.addAttribute("user",userService.queryById(Long.valueOf(user.getUid())));
        model.addAttribute("welcome", "<span style='color: red; font-size: 12px'>Welcome</span> to our <em>fantastic</em> grocery store!");
        return "user/show";
    }
    @RequestMapping("show")
    public String show(User user,Model model){
        log.info("数据展示页面");
        List<User> users = userService.queryAll();
        model.addAttribute("users",users);
        model.addAttribute("welcome", "<span style='color: red; font-size: 12px'>Welcome</span> to our <em>fantastic</em> grocery store!");
        return "user/showtable";
    }
    @RequestMapping("/toSave")
    public String toSave(){
        return "user/save";
    }
    @RequestMapping("/insert")

    public String insert(User user,Model model){
        userService.saveUser(user);
        log.info("开始保存了");
        int i= 1/0;
        User user1 = userService.queryById(Long.valueOf(user.getUid()));
        model.addAttribute("user",user1);
        return "user/show";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Long id,Model model){
        userService.deleteUserById(id);
        return "forward:/user/show";
    }

    //通过yml文件获取数据
    @Value("${page.size}")
    String pageSize;

    @RequestMapping("queryByPage/{curPage}")
    public String queryByPage(@PathVariable("curPage") Integer curPage,Model model){

        if(curPage<1)
            curPage = 1;
        PageInfo<User> pageInfo = userService.getPageInfo(curPage,Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        return "user/showByPage";
    }
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

}

